//
//  LatestJokeViewController.m
//  SmileWeather
//
//  Created by lanou3g on 15/9/16.
//  Copyright (c) 2015年 陶天海. All rights reserved.
//

#import "LatestJokeViewController.h"

@interface LatestJokeViewController ()
@property (nonatomic, strong) NSMutableArray *JokesArray;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic,retain) MBProgressHUD * hud;
//设置loading
- (void)p_setupProgressHud;

@end

@implementation LatestJokeViewController

- (void)reloadLatestJokes {


    [LORequestManger GET:kLatestJokes success:^(id response) {
        NSDictionary  *dic = (NSDictionary *)response;
        NSDictionary *Dic = dic[@"result"];
        for (NSDictionary *pic in Dic[@"data"]) {
            LatestJokeModel  *JokesModel = [LatestJokeModel shareJsonWithDictionary:pic];
            NSLog(@"________%@", JokesModel);
           // JokesModel.isSelect = NO;
            
            [self.JokesArray addObject:JokesModel];
            
            
        }
        
         [_hud hide:YES];
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
        NSLog(@"%@",error);
        
    }];
    


}
- (NSMutableArray *)JokesArray {
    if (!_JokesArray) {
        _JokesArray = [[NSMutableArray alloc] init];
    }

    return _JokesArray;


}


- (void)p_setupProgressHud
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.minSize = CGSizeMake(50, 50);
    _hud.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_hud];
    
    [_hud show:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    
    BlurImageView *blurimageView = [[BlurImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.tableView.backgroundView = blurimageView;
    UIImage * image = [[UIImage imageNamed:@"backimage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(clickLeft:)];
    self.navigationItem.leftBarButtonItem = leftItem;
     self.tableView.separatorStyle =  0;
    [self.tableView registerClass:[JokeTableCell class] forCellReuseIdentifier:@"jokeCell"];
    //设置loading
    [self p_setupProgressHud];
    [self reloadLatestJokes];
    
    __block LatestJokeViewController *root = self;
    [self.tableView addHeaderWithCallback:^{
        NSLog(@"111");
       
        [root.tableView reloadData];
        [root.tableView headerEndRefreshing];
    }];

    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)clickLeft:(UIBarButtonItem *)button {

    [self dismissViewControllerAnimated:NO completion:nil];



}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.JokesArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  JokeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jokeCell" forIndexPath:indexPath];
    
   // NSArray * arr = @[@"首页࿐",@"今日运势࿐",@"今日笑笑࿐",@"我的收藏࿐",@"设置࿐"];
    NSArray * arr = @[@"开心一刻送给珍贵的你࿐",@"笑死你不偿命࿐",@"你有这些智慧么࿐",@"放松一些,体会精彩人生࿐",@"更多精彩尽在笑笑天气࿐"];
    int a = arc4random()%5;
    cell.showLabelOne.title.text = arr[a];
    LatestJokeModel  *JokesModel = self.JokesArray[indexPath.row];
    
    if (JokesModel.content.length == 0) {
          cell.JokesIntroduce.text = @"该条数据消失了...肿么办";
    }
    else {
        cell.JokesIntroduce.text = JokesModel.content;
    }
    
    [cell.showLabelTwo addTarget:self action:@selector(clickButton:) forControlEvents:(UIControlEventTouchUpInside)];
    cell.showLabelTwo.tag = 1000 + indexPath.row;
    
    cell.showLabelTwo.selected = NO;
     NSMutableArray *modelArray = [[PlayerDataBase shareDataBase] selegetAllFromJokeTable];
    
        for (LatestJokeModel *model in modelArray) {
            if ([JokesModel.content isEqualToString:model.content]) {
                cell.showLabelTwo.selected = YES;
            }
    
        }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kWidth/2;
}

- (void)clickButton:(UIButton *)button {
    button.selected = !button.selected;
    
    
    
    
    if (button.selected == YES) {
        //显示alertView提示用户
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
        
        //0.3秒后alertView消失
        [self performSelector:@selector(p_removeAlertView:) withObject:alertView afterDelay:0.3];   }
    


    [[PlayerDataBase shareDataBase] createTables];
    [[PlayerDataBase shareDataBase]insertJokeModelToJokeTable:self.JokesArray[button.tag - 1000]];
    
    
    
  

    
   // NSMutableArray *modelArray = [[PlayerDataBase shareDataBase] selegetAllFromJokeTable];
    
//    for (LatestJokeModel *playModel in modelArray) {
//        NSLog(@"%@",playModel.content);
//        
//    }
//    
   

}
- (void)p_removeAlertView:(UIAlertView *)alertView
{
    //alertView消失
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


      NSLog(@"%@",indexPath);
    


}


//- (void)dealloc
//{
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"collect" object:nil];
//}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
